from app.extensions import get_db
from app.models.Clas import Clas
from app.schames.Clas import ClasSchema
from app.services.ClasServiceFast import ClasService
from fastapi import APIRouter
from fastapi.responses import JSONResponse

clas_router = APIRouter(prefix="/clas", tags=["Clas"])

@clas_router.get("/", summary="获取班级")
def get_by_keyword(keyword:str):
    with get_db() as db: # 链接数据库
        # 创建service对象并调用服务
        svc = ClasService(db)
        response = svc.get_by_keyword(keyword)
        return JSONResponse(
            content={
                "data": [ClasSchema.model_validate(item).model_dump() for item in response["data"]],
                "message": response["message"]
            },
            status_code=response["code"]
        )

def add_clas(clas_name):
    with get_db() as db:  # 链接数据库
        svc = ClasService(db)
        response = svc.add_clas(clas_name)
        return response

def update_clas(clas_id, new_name):
    with get_db() as db:  # 链接数据库
        svc = ClasService(db)
        response = svc.update_clas(clas_id, new_name)
        return response

def delete_clas(clas_id):
    with get_db() as db:  # 链接数据库
        svc = ClasService(db)
        response = svc.delete_clas(clas_id)
        return response